package cn.lisens.demo.mapper;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import cn.lisens.demo.entity.User;
import cn.lisens.demo.mybatis.type.AESEncryptTypeHandler;

@Mapper
public interface UserMapper {

	@Results({
		@Result(column = "phone", property = "phone",
				javaType = String.class, typeHandler = AESEncryptTypeHandler.class)
	})
	@Select("SELECT * FROM user WHERE id = #{id}")
	User findById(Long id);
	
	@Insert("INSERT INTO user(fullname, idcard, phone) VALUES(#{fullname}, #{idcard}, #{phone, typeHandler=cn.lisens.demo.mybatis.type.AESEncryptTypeHandler})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void insert(User user);
    
    @Update("UPDATE user SET fullname=#{fullname}, idcard=#{idcard} WHERE id=#{id}")
    void update(User user);
    
    @Delete("DELETE FROM user WHERE id=#{id}")
    void delete(Long id);
    
    @Select("SELECT count(1) FROM user WHERE id = #{id}")
    Long findCount();
}
